package com.haiyou.common.util.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;


public final class TimeUtil {
	public static final long SECOND = 1000L;
	public static final long FIVE_SECOND = 1000L * 20;
	public static final long MINUTE = 60 * SECOND;
	public static final long HOUR = 60 * MINUTE;
	public static final long DAY = 24 * HOUR;
	public static final long WEEK = 7 * DAY;
	public static final long YEAR = 365 * 24 * HOUR;
	public static final long MONTH = 30 * 24 * HOUR;
	public static final long ABOUT_DAY = DAY - MINUTE * 10;
	private static final long EIGHT_HOUR = HOUR * 8;
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static final SimpleDateFormat dateFormat0 = new SimpleDateFormat("yyyyMMdd");
	private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat datetimeFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
	private static final SimpleDateFormat datetimeFormat2 = new SimpleDateFormat("yyyyMMddHHmmss");
	public static int FOUR_TIME = 0;
	
	/**
	 * 根据日期返回 yyyy-MM-dd HH:mm:ss 格式字符串
	 * @param date
	 * @return
	 */
	public static final String formatYMDhms(Date date) {
		return date == null ? "" : datetimeFormat.format(date);
	}
	
	public static final String formatYMDhmsBySimple(Date date){
		return date == null ? "" : datetimeFormat2.format(date);
	}
	
	/**
	 * 根据日期返回 yyyy-MM-dd HH:mm:ss SSS格式字符串
	 * @param date
	 * @return
	 */
	public static final String formatYMDhmsS(Date date) {
		return date == null ? "" : datetimeFormat1.format(date);
	}

	/**
	 * 根据日期返回 yyyy-MM-dd 格式字符串
	 * @param date
	 * @return
	 */
	public static final String formatYMD(Date date) {
		return date == null ? "" : dateFormat.format(date);
	}
	
	/**
	 * 将字符串yyyy-MM-dd HH:mm:ss解析成Date
	 */
	public static Date parseYMDhms(String datetime){
		if(datetime != null){
			try{
				return datetimeFormat.parse(datetime);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static Date parseYMDhmsBySimple(String datetime){
		if(datetime != null){
			try{
				return datetimeFormat2.parse(datetime);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 将字符串yyyy-MM-dd HH:mm:ss SSS解析成Date
	 */
	public static Date parseYMDhmsS(String datetime){
		if(datetime != null){
			try{
				return datetimeFormat1.parse(datetime);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 将字符串yyyy-MM-dd 解析成Date
	 */
	public static Date parseYMD(String datetime){
		if(datetime != null){
			try{
				return dateFormat.parse(datetime);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 增加时间天数
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addDay(Date date, int amount){
		if(date == null || amount == 0){
			return date;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, amount);
		return cal.getTime();
	}

	/**
	 * 是否同一天
	 */
	public static boolean isSameDay(Date time1, Date time2) {
		if (time1 == null || time2 == null) {
			return false;
		}
		return (time1.getTime() + EIGHT_HOUR) / DAY == (time2.getTime() + EIGHT_HOUR) / DAY;
	}
	
	/**
	 * 是否同一天
	 */
	public static boolean isSameDay(long time1, long time2) {
		if(time1 < DAY || time2 < DAY){
			return false;
		}
		return (time1 + EIGHT_HOUR) / DAY == (time2 + EIGHT_HOUR) / DAY;
	}
	
	/**
	 * 是否同月
	 */
	public static boolean isSameMonth(Date date1, Date date2) {
		if (date1 == null || date2 == null) {
			return false;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(date2);
		return calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)
				&& calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH);
	}
	
	/**
	 * 是否同周
	 */
	public static boolean isSameWeek(Date date1, Date date2) {
		if (date1 == null || date2 == null){
			return false;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		if(calendar.get(Calendar.DAY_OF_WEEK) == 1){
			calendar.add(Calendar.DATE, -7);
		}
		calendar.set(Calendar.DAY_OF_WEEK, 2);
		long time = calendar.getTimeInMillis();
		calendar.setTime(date2);
		if(calendar.get(Calendar.DAY_OF_WEEK) == 1){
			calendar.add(Calendar.DATE, -7);
		}
		calendar.set(Calendar.DAY_OF_WEEK, 2);
		return (time + EIGHT_HOUR) / DAY == (calendar.getTimeInMillis() + EIGHT_HOUR) / DAY;
	}
	
	public static boolean isSameDate(Date time1, Date time2) {
		if(time1 == null){
			return time2 == null;
		}else if(time2 != null){
			return time1.getTime() / SECOND == time2.getTime() / SECOND;
		}
		return false;
	}

	/**
	 * 是否今天
	 */
	public static boolean isToday(long time) {
		return isSameDay(time, System.currentTimeMillis());
	}

	/**
	 * 是否今天
	 */
	public static boolean isToday(Date time) {
		return isSameDay(time, new Date());
	}
	
	/**
	 * 当前是否在今天区间内
	 */
	public static boolean isTimeRange(int beginHour, int endHour)  {
		Calendar calendar = Calendar.getInstance();
		return beginHour <= calendar.get(Calendar.HOUR_OF_DAY)  && calendar.get(Calendar.HOUR_OF_DAY) + 1 <= endHour;
	}
	
	/**
	 * 时间是否在今天区间内
	 */
	public static boolean isTimeRange(int beginHour, int endHour, Date date)  {
		if(date == null || (System.currentTimeMillis() + EIGHT_HOUR) / DAY != (date.getTime() + EIGHT_HOUR) / DAY){
			return false;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return beginHour <= calendar.get(Calendar.HOUR_OF_DAY) && calendar.get(Calendar.HOUR_OF_DAY) <= endHour;
	}
	
	/**
	 * 当前是否在某段时间内
	 */
	public static boolean isTimeRange(Date date,int starDay,int starHour,int endDay, int endHour)  {
		Date startDate = TimeUtil.getDateByWeekDay(date,starDay, starHour, 0, 0);
		Date endDate = TimeUtil.getDateByWeekDay(date,endDay, endHour, 0, 0);
		if(date.getTime() > endDate.getTime() 
				|| date.getTime() < startDate.getTime()){
			return false;
		}
		return true;
	}
	
	/**
	 * 当前是否在某段时间内
	 */
	public static boolean isTimeRange(Date date,Date startDate,Date endDate)  {
		if(date.getTime() > endDate.getTime() 
				|| date.getTime() < startDate.getTime()){
			return false;
		}
		return true;
	}
	
	/**
	 * 获取间隔毫秒
	 */
	public static long getIntevalMillisecond(Date date1, Date date2) {
		if(date1 == null || date2 == null){
			return 0;
		}
		return Math.abs(date1.getTime() - date2.getTime());
	}
	
	/**
	 * 获取间隔秒数
	 */
	public static int getIntervalSecond(Date date1, Date date2){
		if(date1 == null || date2 == null){
			return 0;
		}
		return Math.abs((int)((date1.getTime() - date2.getTime()) / SECOND));
	}
	
	/**
	 * 获取间隔秒数
	 */
	public static int getIntervalSecond(long time1, long time2){
		if(time1 < DAY || time2 < DAY){
			return 0;
		}
		return Math.abs((int)((time1 - time2) / SECOND));
	}

	/**
	 * 获取间隔分钟
	 */
	public static float getIntervalMinute(Date date1, Date date2) {
		if(date1 == null || date2 == null){
			return 0;
		}
		return Math.abs((int)((date1.getTime() - date2.getTime()) / MINUTE));
	}
	
	/**
	 * 获取间隔小时
	 */
	public static float getIntervalHour(Date date1, Date date2) {
		if(date1 == null || date2 == null){
			return 0;
		}
		return Math.abs((int)((date1.getTime() - date2.getTime()) / HOUR));
	}
	
	/**
	 * 获取间隔天数
	 */
	public static int getIntervalDay(Date date1, Date date2){
		if(date1 == null || date2 == null){
			return 0;
		}
		return Math.abs((int)((date1.getTime() - date2.getTime()) / DAY));
	}
	
	/**
	 * 获取间隔天数
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static int getIntervalDay(long time1, long time2){
		if(time1 < DAY || time2 < DAY){
			return 0;
		}
		return Math.abs((int)((time1 - time2) / DAY));
	}
	
	public static int getIntervalWeek(Date date){
		if(date == null){
			return 0;
		}
		return (int) ((System.currentTimeMillis() - date.getTime()) / WEEK);
	}
	
	public static int getIntervalWeek(Date date1, Date date2){
		if(date1 == null || date2 == null){
			return 0;
		}
		return Math.abs((int)((date1.getTime() - date2.getTime()) / WEEK));
	}
	
	public static int getIntervalWeek(long time1, long time2){
		if(time1 <= 0 || time2 <= 0){
			return 0;
		}
		return Math.abs((int)((time1 - time2) / WEEK));
	}
	
//	/**
//	 * 获取时间距离现在第几周
//	 * 算出来的准确(date时间也算一周，到下一周就是第二周)
//	 */
//	@Deprecated
//	public static int getDistanceWeek(Date date){
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(date);
//		calendar.set(Calendar.DAY_OF_WEEK, 2);
//		calendar.set(Calendar.HOUR_OF_DAY, 0);
//		calendar.set(Calendar.MINUTE, 0);
//		calendar.set(Calendar.SECOND, 0);
//		calendar.set(Calendar.MILLISECOND, 0);
//		return (int) ((System.currentTimeMillis() - calendar.getTimeInMillis()) / WEEK) + 1;
//	}

	public static int getDistanceWeek(long time,long now){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time);
		c.set(Calendar.HOUR_OF_DAY,0);
		c.set(Calendar.MINUTE,0);
		c.set(Calendar.SECOND,0);
		c.set(Calendar.MILLISECOND,0);
		int week = c.get(Calendar.DAY_OF_WEEK);
		week = week == Calendar.SUNDAY ? Calendar.SATURDAY : week - Calendar.SUNDAY;
		c.add(Calendar.DAY_OF_MONTH,1-week);
		return (int) ((now - c.getTimeInMillis()) / WEEK + 1);
	}

	/**
	 * 介于两个时间时间 
	 */
	public static boolean between(Date date, Date beginDate, Date endDate) {
		if(date == null || beginDate == null || endDate == null){
			return false;
		}
		return beginDate.getTime() <= date.getTime() && date.getTime() <= endDate.getTime();
	}

	/**
	 * 获得今天某个时间点
	 */
	public static final Date getDate(int hour, int minute, int second) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获得今天某个时间点
	 */
	public static Date getDate(int hour, int minute, int second, int milliScond) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, milliScond);
		return calendar.getTime();
	}
	
	/**
	 * 获取时间当天的某个点
	 */
	public static final Date getDate(Date date, int hour, int minute, int second) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	public static final Date getMonthDayDate(int day, int hour, int minute, int second) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.DATE, day);
		return calendar.getTime();
	}
	
	public static final Date getMonthDayDateByAddMonth(int addMonth, int day, int hour, int minute, int second) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.DATE, day);
		calendar.add(Calendar.MONTH, addMonth);
		return calendar.getTime();
	}
	
	/**
	 * 获取00:00至date的累计分钟数
	 */
	public static int getMinute(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int minute = cal.get(Calendar.HOUR_OF_DAY) * 60;
		minute += cal.get(Calendar.MINUTE);
		return minute;
	}

	/**
	 * 获取时间月份
	 */
	public static final int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return (int) calendar.get(Calendar.MONTH) + 1;
	}
	
	/**
	 * 取得时间当月天数
	 */
	public static final int getMonthDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		calendar.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
		return calendar.get(Calendar.DATE);
	}
	
	/**
	 * 获取时间周几
	 */
	public static int getWeekDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
		return weekDay == 1 ? 7 : weekDay - 1;
	}
	
	/**
	 * 获取本周周几(1-7)
	 */
	public static int getWeekDay(){
		Calendar calendar = Calendar.getInstance();
		int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
		return weekDay == 1 ? 7 : weekDay - 1;
	}

	public static int getWeekDay(long now){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(now);
		int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
		return weekDay == Calendar.SUNDAY ? Calendar.SATURDAY : weekDay - 1;
	}

	/**
	 * 获取本周几某个时间点
	 */
	public static Date getDateByWeekDay(int weekDay, int hour, int minute, int second){
		Calendar calendar = Calendar.getInstance();
		weekDay = weekDay < 0 ? 1 : (weekDay > 7 ? 7 : weekDay);
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		day = day == 1 ? 7 : day - 1;
		if(day != weekDay){
			if(day > weekDay){
				calendar.add(Calendar.DATE, -(day - weekDay));
			}else{
				calendar.add(Calendar.DATE, weekDay - day);
			}
		}
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 获取时间当周，周几某个时间点
	 */
	public static Date getDateByWeekDay(Date date, int weekDay, int hour, int minute, int second){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		weekDay = weekDay < 0 ? 1 : (weekDay > 7 ? 7 : weekDay);
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		day = day == 1 ? 7 : day - 1;
		if(day != weekDay){
			if(day > weekDay){
				calendar.add(Calendar.DATE, -(day - weekDay));
			}else{
				calendar.add(Calendar.DATE, weekDay - day);
			}
		}
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 获取时间N周后的周几时间点
	 */
	public static Date getDateByNextWeekDay(Date date, int nextWeek, int weekDay, int hour, int minute, int second){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		weekDay = weekDay < 0 ? 1 : (weekDay > 7 ? 7 : weekDay);
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		day = day == 1 ? 7 : day - 1;
		if(day != weekDay){
			if(day > weekDay){
				calendar.add(Calendar.DATE, -(day - weekDay));
			}else{
				calendar.add(Calendar.DATE, weekDay - day);
			}
		}
		if(nextWeek > 0){
			calendar.add(Calendar.DATE, nextWeek * 7);
		}
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 获取下个时间
	 * @param date
	 * @param type
	 * @return
	 */
	public static Date getNextDate(Date date, int type, int num){
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);  
		calendar.add(type, num);  
		return calendar.getTime();
	}

	/**
	 * 获取时间N天后的时间
	 */
	public static Date getNextDate(Date date, int day){
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);  
		calendar.add(Calendar.DATE, day);  
		return calendar.getTime();
	}
	
	/**
	 * 获取时间N天前的时间
	 * @param date
	 * @return
	 */
	public static Date getBeforeDate(Date date, int day){
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);  
		calendar.add(Calendar.DATE, -day);  
		return calendar.getTime();
	}
	
	public static Date getNextDate(Date date, int day,int hour, int minute,int second){
		Date nextDate = TimeUtil.addDay(date, day);
		return TimeUtil.getDate(nextDate, hour, minute, second);
	}
	
	/**
	 * 获取从默认时间开始累计天数
	 */
	public static int getCountDay(Date date){
		return (int) ((date.getTime() + EIGHT_HOUR) / DAY);
	}
	
	public static Date getThisWeekDay(Date date,int day,int hour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		cal.setFirstDayOfWeek(day);
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - cal.get(Calendar.DAY_OF_WEEK));
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	 public static Date getDayHour(Date date, int day, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
	 }
	
	 /**
	  * 判断是否跨天 > 0   获取当前是第几天就要+1
	  * @param date1
	  * @param date2
	  * @return
	  */
	public static long betweenDay4Clock(Date date1, Date date2) {
        // 当前时间基准线
        Date beginDate = null;
        Date endDate = null;
        if (date1.getTime() > date2.getTime()) {
            endDate = beginDate(date1, getDayHour(date1, 0, FOUR_TIME));
            beginDate = beginDate(date2, getDayHour(date2, 0, FOUR_TIME));
        } else {
        	endDate = beginDate(date2, getDayHour(date2, 0, FOUR_TIME));
            beginDate = beginDate(date1, getDayHour(date1, 0, FOUR_TIME));
        }
        return between(beginDate, endDate, DateUnit.DAY);
    }
	 /**
	  * 判断是否跨天 > 0   获取当前是第几天就要+1
	  * @param date1
	  * @param date2
	  * @return
	  */
	public static long betweenDayOtherClock(Date date1, Date date2,int hour) {
		  // 当前时间基准线
        Date beginDate = null;
        Date endDate = null;
        if (date1.getTime() > date2.getTime()) {
        	endDate = beginDateOtherClock(date2, getDayHour(date2, 0, hour),hour);
            beginDate = beginDateOtherClock(date1, getDayHour(date1, 0, hour),hour);
        } else {
        	endDate = beginDateOtherClock(date2, getDayHour(date2, 0, hour),hour);
            beginDate = beginDateOtherClock(date1, getDayHour(date1, 0, hour),hour);
        }
        return between(beginDate,endDate, DateUnit.DAY);
    }
	
	 public static Date beginDate(Date date, Date orginDate) {
        if (isSameDay(date, orginDate) ) {
        	if(date.getTime() < orginDate.getTime()){
        		return getDayHour(date, -1, FOUR_TIME);
        	}else{
        		return orginDate;
        	}
        }else {
            return getDayHour(date, 0, FOUR_TIME);
        }
	}
	
	 public static Date beginDateOtherClock(Date date, Date orginDate,int hour) {
        if (isSameDay(date, orginDate)) {
        	if(date.getTime() < orginDate.getTime()){
        		return getDayHour(date, -1, hour);
        	}else{
        		return orginDate;
        	}
        }  else {
            return getDayHour(date, 0, hour);
        }
	}
	 
	public static long between(Date beginDate, Date endDate, DateUnit unit) {
		return between(beginDate, endDate, unit, true);
	}

	public static long between(Date beginDate, Date endDate, DateUnit unit, boolean isAbs) {
		return new DateBetween(beginDate, endDate, isAbs).between(unit);
	}
	
	
    public static Date getNextWeekDate (Date date,int weekNum,int weekDay,int hour){
        try {
        	Date weekNowDate = getDateByWeekDay(date, weekDay, hour, 0, 0);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(weekNowDate);
            calendar.add(Calendar.WEDNESDAY, weekNum - 1);
            return calendar.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

	/**
	 * 当前是否在某段时间内
	 */
	public static boolean isTimeRangeAcrossWeek(Date date ,int starDay,int starHour,int endDay ,int endHour)  {
		if(starDay > endDay){
			if(!TimeUtil.isTimeRange(date, starDay, starHour, 7, 23) && !TimeUtil.isTimeRange(date, 1, 0, endDay, endHour)){
				return false;
			}
		}else{
			if(!TimeUtil.isTimeRange(date, starDay, starHour, endDay, endHour)){
				return false;
			}
		}
		return true;
	}
	/**
	 *	获取下个月一号的时间 
	 */
	public static Date getPerFirstDayOfMonth(int hour, int minute, int second) {
	    Calendar calendar = Calendar.getInstance();
	    calendar.add(Calendar.MONTH, 1);
	    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
	    calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.MILLISECOND, 0);
	    return calendar.getTime();
	}
	
	/**
	 *	获取当前月一号的时间 
	 */
	public static Date getDayOfMonth(int hour, int minute, int second) {
	    Calendar calendar = Calendar.getInstance();
	    calendar.add(Calendar.MONTH, 0);
	    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
	    calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.MILLISECOND, 0);
	    return calendar.getTime();
	}
	

	// 计算两个时间相差几个月
	public static int getMonthCount(Date start, Date end) {
		if (start.after(end)) {
			Date t = start;
			start = end;
			end = t;
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(start);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(end);
		Calendar temp = Calendar.getInstance();
		temp.setTime(end);
		temp.add(Calendar.DATE, 1);
		int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
		int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
		if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) == 1)) {
			return year * 12 + month + 1;
		} else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) {
			return year * 12 + month;
		} else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) {
			return year * 12 + month;
		} else {
			return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
		}
	}

	public static int getMonthCount(Date date) {
		if(date == null){
			return 0;
		}
		return getMonthCount(new Date(), date);
	}
	
	public static Date getThisWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		int day = cal.get(Calendar.DAY_OF_WEEK);
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		return cal.getTime();
	}
	
	public static Date getThisWeekMonday(Date date, int hour) {
		Date monday = getThisWeekMonday(date);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(monday.getTime());
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
 
	public static Date getNextWeekMonday(Date date,int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, day);	
		return cal.getTime();
	}
	
	public static Date getNextWeekMonday(Date date,int day,int hour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, day);	
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	/**
	 * 获取下个时间
	 * @param date
	 * @param type
	 * @return
	 */
	public static Date getLastDate(Date date, int type, int num){
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);  
		calendar.add(type,-num);  
		return calendar.getTime();
	}
	
	/**
	 * 时间段的比较处理 , 如果包含了传来的 时段 了, 就说明时间冲突了
	 * 
	 * @return
	 */
	public static boolean isNotContain(Date s1,Date e1,Date s2,Date e2) {
		long s1Time = s1.getTime();
		long e1Time = e1.getTime();
		long s2Time = s2.getTime();
		long e2Time = e2.getTime();
		if(s1Time < s2Time && e1Time <= s2Time){
			return true;
		}
		if(s2Time < s1Time && e2Time <= s1Time){
			return true;
		}
//		if (s1Time == s2Time && e1Time == e2Time)
//			return true;
		return false;
	}

	/**
	 * 获取当前某个时间点重置时间
	 * @param hour
	 * @param minute
	 * @param second
	 * @return
	 */
	public static Date getResetDate(int hour, int minute, int second){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.MILLISECOND, 0);
		if(calendar.getTimeInMillis() > System.currentTimeMillis()){
			calendar.add(Calendar.DATE, -1);  
		}
		return calendar.getTime();
	}

	/**
	 * 获取date当前重置时间
	 * @param date
	 * @param hour
	 * @param minute
	 * @param second
	 * @return
	 */
	public static Date getResetDate(Date date, int hour, int minute, int second){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if(calendar.get(Calendar.HOUR_OF_DAY) < hour){
			calendar.add(Calendar.DATE, -1);  
		}
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取重置时间 N天后
	 * @param time
	 * @param day
	 * @param hour
	 * @param minute
	 * @param second
	 * @return
	 */
	public static Date getResetTimeNextDay(Date time, int day, int hour, int minute, int second){
		if(time == null){
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		if(calendar.get(Calendar.HOUR_OF_DAY) < hour){
			calendar.add(Calendar.DATE, -1);  
		}
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.add(Calendar.DAY_OF_YEAR, day);
		return calendar.getTime();
	}

	public static Date getResetDateByWeekDay(int weekDay, int hour, int minute, int second){
		Calendar calendar = Calendar.getInstance();
		weekDay = weekDay < 0 ? 1 : (weekDay > 7 ? 7 : weekDay);
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		day = day == 1 ? 7 : day - 1;
		if(day != weekDay){
			if(day > weekDay){
				calendar.add(Calendar.DATE, -(day - weekDay));
			}else{
				calendar.add(Calendar.DATE, weekDay - day);
			}
		}
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, 0);
		if(calendar.getTimeInMillis() > System.currentTimeMillis()){
			calendar.add(Calendar.DATE, -7);  
		}
		return calendar.getTime();
	}

	public static int format0(Date date){
		return Integer.parseInt(dateFormat0.format(date));
	}

	public static long nextWeekFirstDay(long now){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(now);
		c.set(Calendar.HOUR_OF_DAY,0);
		c.set(Calendar.MINUTE,0);
		c.set(Calendar.SECOND,0);
		c.set(Calendar.MILLISECOND,0);
		int week = c.get(Calendar.DAY_OF_WEEK);
		week = week == Calendar.SUNDAY ? Calendar.SATURDAY : week - Calendar.SUNDAY;
		c.add(Calendar.DAY_OF_MONTH,Calendar.SATURDAY-week + Calendar.SUNDAY + (1-Calendar.SUNDAY)*Calendar.SATURDAY);
		return c.getTimeInMillis();
	}

	public static long currWeekFirstDay(long now){
		return nextWeekFirstDay(now) - WEEK;
	}

	public static long weekFirstDay(){
		return currWeekFirstDay(System.currentTimeMillis());
	}

	public static int preWeekFirstDay0(long now){
		return format0(new Date(nextWeekFirstDay(now) - WEEK - WEEK));
	}

	public static int currWeekFirstDay0(long now){
		return format0(new Date(nextWeekFirstDay(now) - WEEK));
	}

	public static long todayZero(){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY,0);
		c.set(Calendar.MINUTE,0);
		c.set(Calendar.SECOND,0);
		c.set(Calendar.MILLISECOND,0);
		return c.getTimeInMillis();
	}

	public static Calendar timeZero(long time){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time);
		c.set(Calendar.HOUR_OF_DAY,0);
		c.set(Calendar.MINUTE,0);
		c.set(Calendar.SECOND,0);
		c.set(Calendar.MILLISECOND,0);
		return c;
	}

	public static long endTime(long begin,int keepDay){
		return begin + keepDay * DAY;
	}

	public static int diffDay(long time1,long time2){
		Calendar c1 = timeZero(time1);
		Calendar c2 = timeZero(time2);
		return (int) (Math.abs(c1.getTimeInMillis() - c2.getTimeInMillis()) / DAY + 1);
	}


	public static void main(String[] args) throws ParseException {
//		Date d = new Date(System.currentTimeMillis() - DAY*0);
//		System.out.println(formatYMDhms(d));
//		System.out.println(getDistanceWeek(d.getTime(),System.currentTimeMillis()));

//		System.out.println(currWeekFirstDay(System.currentTimeMillis()));
		System.out.println(betweenDay4Clock(new Date(),new Date()));
	}

}
